package com.paireach.bw.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.paireach.bw.R;
import com.paireach.bw.application.MyApplication;
import com.paireach.bw.bean.DriveOrder;
import com.paireach.bw.bean.OrdersBean;
import com.paireach.bw.common.Constant;

import java.util.List;

/**
 * Created by ${cqc} on 2017/3/1.
 * 常规:收货工厂  adapter
 */

public class CommonUnloadAdapter extends RecyclerView.Adapter {
    private int HeaderType = 1;
    private View headerView;

    public void addHeaderView(View headerView) {
        this.headerView = headerView;
    }

    private List<OrdersBean> list;
    private OrdersBean info;
    private MyViewHolder holder;
    private String status;

    public CommonUnloadAdapter(List<OrdersBean> list) {
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HeaderType) {
            return new MyViewHolder(headerView);
        }
        View view = View.inflate(parent.getContext(), R.layout.item_common_unload, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        if (getItemViewType(position) == HeaderType) {
            return;
        }
        holder = (MyViewHolder) viewHolder;
        info = list.get(position - 1);
        holder.tv2.setText("交货单号：" + info.getSHPM_NUM());
        holder.tv3.setText("收货地名称：" + info.getTO_SHPG_LOC_NAME());
        holder.tv4.setText("收货方地址：" + info.getTO_SHPG_ADDR());

        /** enable表示该运单正在运行*/
        String enable = info.getEnable();
        Logger.d(info.getSHPM_NUM() + "---" + enable);
        if ("N".equals(enable)) {
            holder.tv_ok.setEnabled(false);
            holder.tv_no.setEnabled(false);
            holder.tv_ok.setClickable(false);
            holder.tv_no.setClickable(false);
            holder.tv_ok.setBackgroundColor(MyApplication.getContext().getResources().getColor(R.color.grgray));
            holder.tv_no.setBackgroundColor(MyApplication.getContext().getResources().getColor(R.color.grgray));
        } else {
            holder.tv_ok.setEnabled(true);
            holder.tv_no.setEnabled(true);
            holder.tv_ok.setClickable(true);
            holder.tv_no.setClickable(true);
            holder.tv_ok.setBackgroundColor(MyApplication.getContext().getResources().getColor(R.color.color6));
            holder.tv_no.setBackgroundColor(MyApplication.getContext().getResources().getColor(R.color.color6));
        }

        status = info.getSHPM_STATUS();
        if ("230".equals(status)) {
            holder.tv_ok.setText("签到确认");
            holder.tv_no.setVisibility(View.GONE);
        } else if ("238".equals(status)) {
            holder.tv_ok.setText("开始卸货");
            holder.tv_no.setVisibility(View.GONE);
        } else if ("240".equals(status)) {
            holder.tv_ok.setText("正常签收");
            holder.tv_no.setVisibility(View.VISIBLE);
            holder.tv_no.setText("破损/拒收/退货上报");
        } else if ("241".equals(status)) {
            holder.tv_ok.setText("破损/拒收/退货上报");
            holder.tv_ok.setEnabled(false);
            holder.tv_ok.setClickable(false);
            holder.tv_ok.setBackgroundColor(MyApplication.getContext().getResources().getColor(R.color.grgray));
            holder.tv_no.setVisibility(View.GONE);
        } else {
            holder.tv_ok.setEnabled(false);
            holder.tv_ok.setClickable(false);
            holder.tv_ok.setText("已签收");
            holder.tv_ok.setBackgroundColor(MyApplication.getContext().getResources().getColor(R.color.grgray));
            holder.tv_no.setVisibility(View.GONE);
        }

        if (onItemClickListener != null) {
            holder.tv_ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.onItemClick(view, position);
                }
            });
            holder.tv_no.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.onItemClick(view, position);
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return HeaderType;
        }
        return super.getItemViewType(position - 1);
    }

    @Override
    public int getItemCount() {
        return list.size() + 1;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv1;
        TextView tv2;
        TextView tv3;
        TextView tv4;
        TextView tv_ok;
        TextView tv_no;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv1 = (TextView) itemView.findViewById(R.id.tv1);
            tv2 = (TextView) itemView.findViewById(R.id.tv2);
            tv3 = (TextView) itemView.findViewById(R.id.tv3);
            tv4 = (TextView) itemView.findViewById(R.id.tv4);
            tv_ok = (TextView) itemView.findViewById(R.id.btn);
            tv_no = (TextView) itemView.findViewById(R.id.btn2);
        }
    }


    //item点击事件
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

}
